package com.supermarket.business.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.supermarket.business.domain.SupermarketProduct;
import com.supermarket.business.domain.SupermarketSupplier;
import com.supermarket.business.mapper.SupermarketSupplierMapper;
import com.supermarket.business.service.IFileService;
import com.supermarket.business.vo.SupermarketProductVO;
import com.supermarket.common.core.domain.entity.SysDictData;
import com.supermarket.common.utils.DateUtils;
import com.supermarket.system.mapper.SysDictDataMapper;
import com.supermarket.system.service.ISysDictDataService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.supermarket.business.mapper.SupermarketProductMapper;
import com.supermarket.business.service.ISupermarketProductService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

/**
 * 商品Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-07-21
 */
@Service
public class SupermarketProductServiceImpl extends ServiceImpl<SupermarketProductMapper, SupermarketProduct> implements ISupermarketProductService
{
    @Resource
    private SupermarketProductMapper supermarketProductMapper;
    @Resource
    private SupermarketSupplierMapper supermarketSupplierMapper;
    @Resource
    private SysDictDataMapper sysDictDataMapper;
    @Resource
    private ISysDictDataService iSysDictDataService;
    @Resource
    private IFileService fileService;

    private static final String DICT_TYPE_PRODUCT_DICT_TYPE = "shangping";
    private static final String DICT_TYPE_PRODUCT_LIST_CLASS = "default";
    private static final String DICT_TYPE_PRODUCT_IS_DEFAULT = "N";

    /**
     * 查询商品
     * 
     * @param productId 商品主键
     * @return 商品
     */
    @Override
    public SupermarketProduct selectSysProductByProductId(Long productId)
    {
        return supermarketProductMapper.selectSysProductByProductId(productId);
    }

    /**
     * 查询商品列表
     * 
     * @param SupermarketProduct 商品
     * @return 商品
     */
    @Override
    public List<SupermarketProduct> selectSysProductList(SupermarketProduct SupermarketProduct)
    {
        return supermarketProductMapper.selectSysProductList(SupermarketProduct);
    }

    /**
     * 新增商品
     * 
     * @param supermarketProduct 商品
     * @return 结果
     */
    @Override
    @Transactional
    public int insertSysProduct(SupermarketProduct supermarketProduct)
    {
        CheckSupplierId(supermarketProduct.getSupplierId());
        //将商品图片存储到阿里云
        String imageUrl = fileService.uploadFile(supermarketProduct.getFile());
        supermarketProduct.setImageUrl(imageUrl);
        int count = supermarketProductMapper.insertSysProduct(supermarketProduct);
        //新增商品到字典数据中
        SysDictData sysDictData = new SysDictData();
        sysDictData.setDictType(DICT_TYPE_PRODUCT_DICT_TYPE);
        sysDictData.setListClass(DICT_TYPE_PRODUCT_LIST_CLASS);
        sysDictData.setIsDefault(DICT_TYPE_PRODUCT_IS_DEFAULT);
        sysDictData.setStatus("0");
        sysDictData.setDictSort(0L);
        sysDictData.setDictLabel(supermarketProduct.getProductName());
        sysDictData.setDictValue(supermarketProduct.getProductId().toString());
        iSysDictDataService.insertDictData(sysDictData);
        return count;
    }

    /**
     * 修改商品
     * 
     * @param supermarketProduct 商品
     * @return 结果
     */
    @Override
    @Transactional
    public int updateSysProduct(SupermarketProduct supermarketProduct)
    {
        CheckSupplierId(supermarketProduct.getSupplierId());
        //将商品图片存储到阿里云
        String imageUrl = fileService.uploadFile(supermarketProduct.getFile());
        supermarketProduct.setImageUrl(imageUrl);
        //同步字典数据
        SysDictData sysDictData = sysDictDataMapper.selectDictDataByTypeAndValue(DICT_TYPE_PRODUCT_DICT_TYPE, supermarketProduct.getProductId().toString());
        sysDictData.setDictLabel(supermarketProduct.getProductName());
        iSysDictDataService.updateDictData(sysDictData);
        return supermarketProductMapper.updateSysProduct(supermarketProduct);
    }

    /**
     * 批量删除商品
     * 
     * @param productIds 需要删除的商品主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysProductByProductIds(Long[] productIds)
    {
        //删除字典数据
        Long[] dictIds = new Long[productIds.length];
        int i=0;
        for(Long productId : productIds) {
            SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(productId);
            SysDictData sysDictData = sysDictDataMapper.selectDictDataByTypeAndValue(DICT_TYPE_PRODUCT_DICT_TYPE, product.getProductId().toString());
            dictIds[i++] = sysDictData.getDictCode();
        }
        iSysDictDataService.deleteDictDataByIds(dictIds);
        return supermarketProductMapper.deleteSysProductByProductIds(productIds);
    }

    /**
     * 删除商品信息
     * 
     * @param productId 商品主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteSysProductByProductId(Long productId)
    {
        //删除字典数据
        SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(productId);
        SysDictData sysDictData = sysDictDataMapper.selectDictDataByTypeAndValue(DICT_TYPE_PRODUCT_DICT_TYPE, product.getProductId().toString());
        Long[] dictIds = {sysDictData.getDictCode()};
        iSysDictDataService.deleteDictDataByIds(dictIds);
        return supermarketProductMapper.deleteSysProductByProductId(productId);
    }

    /**
     * 获取商品VO列表
     * @param supermarketProduct
     * @return
     */
    @Override
    public List<SupermarketProductVO> selectSysProductListVO(SupermarketProduct supermarketProduct) {
        List<SupermarketProduct> list = supermarketProductMapper.selectSysProductList(supermarketProduct);
        List<SupermarketProductVO> voList = new ArrayList<>();
        for (SupermarketProduct product : list) {
            SupermarketProductVO productVO = new SupermarketProductVO();
            BeanUtils.copyProperties(product, productVO);
            voList.add(productVO);
        }
        return voList;
    }

    @Override
    public SupermarketProductVO selectSysProductVOByProductId(Long productId) {
        SupermarketProduct product = supermarketProductMapper.selectSysProductByProductId(productId);
        if (product == null) {
            throw new IllegalArgumentException("商品不存在");
        }
        SupermarketProductVO productVO = new SupermarketProductVO();
        BeanUtils.copyProperties(product, productVO);
        return productVO;
    }

    private void CheckSupplierId(Long supplierId) {
        SupermarketSupplier supermarketSupplier = supermarketSupplierMapper.selectSysSupplierBySupplierId(supplierId);
        if(supermarketSupplier == null){
            throw new IllegalArgumentException("该供应商id不存在");
        }
    }
}
